package com.ruoyi.eqmonitor.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eqmonitor.dispose.service.IExteriorSiteAlarmService;
import com.ruoyi.eqmonitor.domain.OptHandleFlowRecord;
import com.ruoyi.eqmonitor.domain.OptPatrolTask;
import com.ruoyi.eqmonitor.dtos.*;
import com.ruoyi.eqmonitor.service.IOptDeviceAlarmService;
import com.ruoyi.eqmonitor.service.IOptHandleFlowRecordService;
import com.ruoyi.eqmonitor.service.IOptPatrolTaskService;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvOutletProblemService;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvOverflowWellService;
import com.ruoyi.eqmonitor.vo.CockpitVo;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.ruoyi.eqmonitor.mapper.OptPatrolEventMapper;
import com.ruoyi.eqmonitor.domain.OptPatrolEvent;
import com.ruoyi.eqmonitor.service.IOptPatrolEventService;

import javax.annotation.Resource;

/**
 * 巡查上报事件Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-05
 */
@Service
public class OptPatrolEventServiceImpl extends ServiceImpl<OptPatrolEventMapper,OptPatrolEvent> implements IOptPatrolEventService {
    
    @Autowired
    private OptPatrolEventMapper optPatrolEventMapper;

    @Autowired
    private IOptHandleFlowRecordService optHandleFlowRecordService;

    @Lazy
    @Autowired
    private IOptPatrolTaskService optPatrolTaskService;

    @Autowired
    private IOptDeviceAlarmService iOptDeviceAlarmService;

    @Resource
    private IExteriorSiteAlarmService exteriorSiteAlarmService;

    @Lazy
    @Resource
    private ISmdtvOutletProblemService smdtvOutletProblemService;

    @Lazy
    @Resource
    private ISmdtvOverflowWellService smdtvOverflowWellService;

    @Override
    public OptPatrolEvent selectOptPatrolEventById(Long id){
        return optPatrolEventMapper.selectOptPatrolEventById(id);
    }
      
    @Override
    public List<OptPatrolEvent> selectOptPatrolEventList(OptPatrolEvent optPatrolEvent){
        return optPatrolEventMapper.selectOptPatrolEventList(optPatrolEvent);
    }

    @Override
    public List<OptDeviceAlarmDto> reCockpitData(List<CockpitVo> vos, String type){
        List<OptDeviceAlarmDto> list = new ArrayList<>();
        List<OptPatrolEventDPY> optPatrolEventDPIES = optPatrolEventMapper.selesctDPY();
        for (CockpitVo vo : vos){
            String id = type + vo.getId();
            OptDeviceAlarmDto alarmDto = new OptDeviceAlarmDto();
            for (OptPatrolEventDPY dpy : optPatrolEventDPIES){
                if (id.equals(dpy.getMinorId())){
                    vo.setBusinessId(dpy.getId());
                    vo.setActivityName(dpy.getActivityName());
                    vo.setActivityFlag(dpy.getActivityFlag());
                }
            }
            alarmDto.setCockpitVo(vo);
            list.add(alarmDto);
        }
        return list;
    }

    @Override
    public boolean add(OptPatrolEvent optPatrolEvent) {
        //按日期生成事件编码
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String evtCode = sdf.format(date);
        optPatrolEvent.setCode("TZ" + evtCode);

        LoginUser user = SecurityUtils.getLoginUser();
        if (user != null) {
            optPatrolEvent.setReportPersonId(user.getUserid());
            optPatrolEvent.setReportPersonName(user.getUsername());
        }

        optPatrolEvent.setCreateTime(new Date());
        optPatrolEvent.setCreateBy(SecurityUtils.getUsername());
        boolean isSuccess = save(optPatrolEvent);

        //插入流程表记录——任务待办
        OptHandleFlowRecord optHandleFlowRecord= new OptHandleFlowRecord();
        optHandleFlowRecord.setBusinessId(optPatrolEvent.getId());
        optHandleFlowRecord.setActivityName("事件上报");
        optHandleFlowRecord.setActivityFlag(10L);
        //optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
        //optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
        //optHandleFlowRecord.setHandleTime(new Date());
        optHandleFlowRecord.setCreateTime(new Date());
        //optHandleFlowRecord.setAssignContent("巡查事件上报，请及时处理");
        optHandleFlowRecord.setContent(optPatrolEvent.getContent());
        optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecord);

        OptPatrolTask optPatrolTask = optPatrolTaskService.getById(optPatrolEvent.getTaskId());
        Date createDate = new Date();

        //判断处置单位是否为空,有处置单位则到事件受理
        if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId())) {
            //插入流程表记录——事件受理
            OptHandleFlowRecord optHandleFlowRecordEventAccept = new OptHandleFlowRecord();
            //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
            optHandleFlowRecordEventAccept.setBusinessId(optPatrolEvent.getId());//当前事件Id
            optHandleFlowRecordEventAccept.setActivityName("事件受理");
            optHandleFlowRecordEventAccept.setActivityFlag(11L);
            //optHandleFlowRecordEvent.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
            //optHandleFlowRecordEvent.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
            optHandleFlowRecordEventAccept.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
            optHandleFlowRecordEventAccept.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
            optHandleFlowRecordEventAccept.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
            optHandleFlowRecordEventAccept.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
            //optHandleFlowRecordEvent.setHandleTime(new Date());
            optHandleFlowRecordEventAccept.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
            createDate.setTime(createDate.getTime() + 40);
            optHandleFlowRecordEventAccept.setCreateTime(createDate);
            optHandleFlowRecordEventAccept.setContent("系统自动受理");
            optHandleFlowRecordService.save(optHandleFlowRecordEventAccept);

            optPatrolEvent.setStatus(11L);//事件直接派遣处置单位
            optPatrolEvent.setDisposeDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));//处置部门Id
            optPatrolEvent.setDisposeDeptName(optPatrolTask.getExecuteOrganizationName());//处置部门名称
            optPatrolEvent.setDisposeShouldFinishTime(optPatrolTask.getEndTime());//处置时限时间——应完成时间
            updateById(optPatrolEvent);
        }

        //判断处置单位任务接收人是否为空,有处置单位和任务接收人则到派遣人员
        if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId()) && optPatrolTask.getReceiveUserId() > 0) {
            //插入流程表记录——事件受理
            OptHandleFlowRecord optHandleFlowRecordEventAccept = new OptHandleFlowRecord();
            //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
            optHandleFlowRecordEventAccept.setBusinessId(optPatrolEvent.getId());//当前事件Id
            optHandleFlowRecordEventAccept.setActivityName("派遣人员");
            optHandleFlowRecordEventAccept.setActivityFlag(12L);
            optHandleFlowRecordEventAccept.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
            optHandleFlowRecordEventAccept.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
            optHandleFlowRecordEventAccept.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
            optHandleFlowRecordEventAccept.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
            optHandleFlowRecordEventAccept.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
            optHandleFlowRecordEventAccept.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
            //optHandleFlowRecordEvent.setHandleTime(new Date());
            optHandleFlowRecordEventAccept.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
            createDate.setTime(createDate.getTime() + 50);
            optHandleFlowRecordEventAccept.setCreateTime(createDate);
            optHandleFlowRecordEventAccept.setContent("系统自动派遣");
            optHandleFlowRecordService.save(optHandleFlowRecordEventAccept);

            optPatrolEvent.setStatus(12L);//事件直接到派遣人员
            optPatrolEvent.setObjectId(optPatrolTask.getReceiveUserId());
            optPatrolEvent.setObjectName(optPatrolTask.getReceiveUserName());
            updateById(optPatrolEvent);
        }
        return isSuccess;
    }

    @Override
    public boolean assign(OptPatrolEventAssignDto optPatrolEventAssignDto) {
        OptPatrolEvent optPatrolEvent = getById(optPatrolEventAssignDto.getEvtId());

        //插入流程表记录——事件受理
        OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
        optHandleFlowRecord.setBusinessId(optPatrolEventAssignDto.getEvtId());
        //optHandleFlowRecord.setBusinessId(optPatrolEvent.getId());
        optHandleFlowRecord.setActivityName("事件受理");
        optHandleFlowRecord.setActivityFlag(11L);
        optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
        optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
        optHandleFlowRecord.setHandleTime(new Date());
        optHandleFlowRecord.setCreateTime(new Date());
        optHandleFlowRecord.setHandleDeptId(optPatrolEventAssignDto.getHandleDeptId());
        optHandleFlowRecord.setHandleDeptName(optPatrolEventAssignDto.getHandleDeptName());
        optHandleFlowRecord.setHandleTimeLimit(optPatrolEventAssignDto.getHandleTimeLimit());
        optHandleFlowRecord.setContent(optPatrolEventAssignDto.getAssignContent());
        optHandleFlowRecord.setAssignContent(optPatrolEventAssignDto.getAssignContent());
        //记录原处置单位Id和名称
        if(optPatrolEvent.getDisposeDeptId() == null){
            optHandleFlowRecord.setOldUserId(optPatrolEvent.getDisposeDeptId());
            optHandleFlowRecord.setOldUserName(optPatrolEvent.getDisposeDeptName());
        }

        optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecord);

        optPatrolEvent.setDisposeDeptId(optPatrolEventAssignDto.getHandleDeptId());
        optPatrolEvent.setDisposeDeptName(optPatrolEventAssignDto.getHandleDeptName());
        optPatrolEvent.setDisposeShouldFinishTime(optPatrolEventAssignDto.getHandleTimeLimit());
        optPatrolEvent.setAssignOpinion(optPatrolEventAssignDto.getAssignContent());
        optPatrolEvent.setStatus(11L);//事件受理
        optPatrolEvent.setUpdateTime(new Date());
        optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());

        return updateById(optPatrolEvent);
    }

    @Override
    public boolean dispatch(OptPatrolEventDispatchDto optPatrolEventDispatchDto) {
        OptPatrolEvent optPatrolEvent = getById(optPatrolEventDispatchDto.getEvtId());

        //插入流程表记录——事件受理
        OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
        optHandleFlowRecord.setBusinessId(optPatrolEventDispatchDto.getEvtId());
        //optHandleFlowRecord.setBusinessId(optPatrolEvent.getId());
        optHandleFlowRecord.setActivityName("派遣人员");
        optHandleFlowRecord.setActivityFlag(12L);
        optHandleFlowRecord.setHandleUserId(optPatrolEventDispatchDto.getHandleUserId());//当前登录用户Id
        optHandleFlowRecord.setHandleUserName(optPatrolEventDispatchDto.getHandleUserName());//当前登录用户姓名
        optHandleFlowRecord.setHandleTime(new Date());
        optHandleFlowRecord.setCreateTime(new Date());
        optHandleFlowRecord.setHandleDeptId(optPatrolEventDispatchDto.getHandleDeptId());
        optHandleFlowRecord.setHandleDeptName(optPatrolEventDispatchDto.getHandleDeptName());
        optHandleFlowRecord.setHandleTimeLimit(optPatrolEventDispatchDto.getHandleTimeLimit());
        optHandleFlowRecord.setContent(optPatrolEventDispatchDto.getContent());

        optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecord);

//        optPatrolEvent.setDisposeDeptId(optPatrolEventHandleDto.getHandleDeptId());
//        optPatrolEvent.setDisposeDeptName(optPatrolEventHandleDto.getHandleDeptName());
//        optPatrolEvent.setDisposeShouldFinishTime(optPatrolEventHandleDto.getHandleTimeLimit());
//        optPatrolEvent.setAssignOpinion(optPatrolEventHandleDto.getAssignContent());
        optPatrolEvent.setObjectId(optPatrolEventDispatchDto.getHandleUserId());
        optPatrolEvent.setObjectName(optPatrolEventDispatchDto.getHandleUserName());
        optPatrolEvent.setStatus(12L);//派遣人员
        optPatrolEvent.setUpdateTime(new Date());
        optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
        return updateById(optPatrolEvent);
    }

    @Override
    public boolean confirm(OptPatrolEventConfirmDto optPatrolEventConfirmDto) {
        OptPatrolEvent optPatrolEvent = getById(optPatrolEventConfirmDto.getEvtId());

        if(optPatrolEventConfirmDto.getIsConfirm()) {
            //插入流程表记录——事件确认
            OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
            optHandleFlowRecord.setBusinessId(optPatrolEventConfirmDto.getEvtId());
            optHandleFlowRecord.setActivityName("事件确认");
            optHandleFlowRecord.setActivityFlag(13L);
            if(optPatrolEvent.getObjectId() == null) {
                optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            }
            else{
                optHandleFlowRecord.setHandleUserId(optPatrolEvent.getObjectId());//当前登录用户Id
                optHandleFlowRecord.setHandleUserName(optPatrolEvent.getObjectName());//当前登录用户姓名
            }
            optHandleFlowRecord.setHandleTime(new Date());
            optHandleFlowRecord.setCreateTime(new Date());
            optHandleFlowRecord.setContent(optPatrolEventConfirmDto.getContent());
            optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
            optHandleFlowRecordService.save(optHandleFlowRecord);

            optPatrolEvent.setStatus(13L);//事件确认
            optPatrolEvent.setDisposeFinishTime(new Date());//处置完成时间
            optPatrolEvent.setUpdateTime(new Date());
            optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
            updateById(optPatrolEvent);

            //插入流程表记录——任务处置中
            OptHandleFlowRecord optHandleFlowRecordHandle = new OptHandleFlowRecord();
            optHandleFlowRecordHandle.setTaskId(optPatrolEvent.getTaskId());//当前任务Id
            optHandleFlowRecordHandle.setActivityName("处置中");
            optHandleFlowRecordHandle.setActivityFlag(4L);
            optHandleFlowRecordHandle.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
            optHandleFlowRecordHandle.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            optHandleFlowRecordHandle.setHandleTime(new Date());
            optHandleFlowRecordHandle.setContent("系统自动");
            optHandleFlowRecordHandle.setCreateBy(SecurityUtils.getUsername());
            Date createDate = new Date();
            createDate.setTime(createDate.getTime() + 20);
            optHandleFlowRecordHandle.setCreateTime(createDate);
            optHandleFlowRecordService.save(optHandleFlowRecordHandle);

            OptPatrolTask optPatrolTask = optPatrolTaskService.getById(optPatrolEvent.getTaskId());
            optPatrolTask.setStatusId(4L);
            optPatrolTask.setStatusName("处置中");
            optPatrolTask.setUpdateTime(new Date());
            optPatrolTask.setUpdateBy(SecurityUtils.getUsername());
            return optPatrolTaskService.updateById(optPatrolTask);
        } else {

            //插入流程表记录——事件确认
            OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
            optHandleFlowRecord.setBusinessId(optPatrolEventConfirmDto.getEvtId());
            optHandleFlowRecord.setActivityName("事件确认不符合");
            optHandleFlowRecord.setActivityFlag(13L);
            if(optPatrolEvent.getObjectId() == null) {
                optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            }
            else{
                optHandleFlowRecord.setHandleUserId(optPatrolEvent.getObjectId());//当前登录用户Id
                optHandleFlowRecord.setHandleUserName(optPatrolEvent.getObjectName());//当前登录用户姓名
            }
            optHandleFlowRecord.setHandleTime(new Date());
            optHandleFlowRecord.setCreateTime(new Date());
            optHandleFlowRecord.setContent(optPatrolEventConfirmDto.getContent());
            optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
            optHandleFlowRecordService.save(optHandleFlowRecord);

            //插入流程表记录——事件完结
            OptHandleFlowRecord optHandleFlowRecordEnd = new OptHandleFlowRecord();
            optHandleFlowRecordEnd.setBusinessId(optPatrolEventConfirmDto.getEvtId());
            optHandleFlowRecordEnd.setActivityName("事件完结");
            optHandleFlowRecordEnd.setActivityFlag(19L);
            optHandleFlowRecordEnd.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
            optHandleFlowRecordEnd.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            Date createDate = new Date();
            createDate.setTime(createDate.getTime() + 10);
            optHandleFlowRecordEnd.setFinishTime(createDate);
            optHandleFlowRecordEnd.setCreateTime(createDate);
            optHandleFlowRecordEnd.setContent(optPatrolEventConfirmDto.getContent());
            optHandleFlowRecordEnd.setCreateBy(SecurityUtils.getUsername());
            optHandleFlowRecordService.save(optHandleFlowRecordEnd);

            optPatrolEvent.setStatus(19L);//事件完结
            optPatrolEvent.setEndEvtTime(new Date());
            optPatrolEvent.setEndEvtResult(optPatrolEventConfirmDto.getContent());
            optPatrolEvent.setUpdateTime(new Date());
            optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
            return updateById(optPatrolEvent);
        }
    }

    @Override
    public boolean handle(OptPatrolEventHandleDto optPatrolEventHandleDto) {

        OptPatrolEvent optPatrolEvent = getById(optPatrolEventHandleDto.getEvtId());
        //插入流程表记录——处置反馈
        OptHandleFlowRecord optHandleFlowRecordHandle = new OptHandleFlowRecord();
        optHandleFlowRecordHandle.setBusinessId(optPatrolEventHandleDto.getEvtId());
        optHandleFlowRecordHandle.setActivityName("处置反馈");
        optHandleFlowRecordHandle.setActivityFlag(14L);
        if(optPatrolEvent.getObjectId() == null) {
            optHandleFlowRecordHandle.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
            optHandleFlowRecordHandle.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
        }
        else{
            optHandleFlowRecordHandle.setHandleUserId(optPatrolEvent.getObjectId());//当前登录用户Id
            optHandleFlowRecordHandle.setHandleUserName(optPatrolEvent.getObjectName());//当前登录用户姓名
        }

        optHandleFlowRecordHandle.setHandleDeptId(optPatrolEventHandleDto.getHandleDeptId());
        optHandleFlowRecordHandle.setHandleDeptName(optPatrolEventHandleDto.getHandleDeptName());
        optHandleFlowRecordHandle.setCreateTime(new Date());
        optHandleFlowRecordHandle.setHandleTime(new Date());
        optHandleFlowRecordHandle.setHandleDescription(optPatrolEventHandleDto.getHandleDescription());
        optHandleFlowRecordHandle.setFileIds(optPatrolEventHandleDto.getFileIds());
        optHandleFlowRecordHandle.setVideoIds(optPatrolEventHandleDto.getVideoIds());
        optHandleFlowRecordHandle.setContent(optPatrolEventHandleDto.getHandleContent());
        optHandleFlowRecordHandle.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecordHandle);


        optPatrolEvent.setStatus(14L);//处置反馈
        optPatrolEvent.setDisposeFinishTime(new Date());//处置完成时间
        if(optPatrolEvent.getObjectId() == null)
        {
            optPatrolEvent.setObjectId(SecurityUtils.getUserId());//当前登录用户Id
            optPatrolEvent.setObjectName(SecurityUtils.getUsername());//当前登录用户姓名
        }
        if(optPatrolEvent.getDisposeDeptId() == null){
            optPatrolEvent.setDisposeDeptId(optPatrolEventHandleDto.getHandleDeptId());
            optPatrolEvent.setDisposeDeptName(optPatrolEventHandleDto.getHandleDeptName());
        }

        optPatrolEvent.setDisposeImgIds(optPatrolEventHandleDto.getFileIds());
        optPatrolEvent.setDisposeVideoIds(optPatrolEventHandleDto.getVideoIds());
        optPatrolEvent.setDisposeRecordingIds(optPatrolEventHandleDto.getDisposeRecordingIds());
        optPatrolEvent.setDisposeContent(optPatrolEventHandleDto.getHandleContent());
        optPatrolEvent.setDisposeDescription(optPatrolEventHandleDto.getHandleDescription());
        optPatrolEvent.setUpdateTime(new Date());
        optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
        updateById(optPatrolEvent);


        //插入流程表记录——主管单位审核
        OptHandleFlowRecord optHandleFlowRecordAudit = new OptHandleFlowRecord();
        optHandleFlowRecordAudit.setBusinessId(optPatrolEventHandleDto.getEvtId());
        optHandleFlowRecordAudit.setActivityName("主管单位审核");
        optHandleFlowRecordAudit.setActivityFlag(17L);
        optHandleFlowRecordAudit.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
        optHandleFlowRecordAudit.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
//        optHandleFlowRecordAudit.setHandleDeptId(optPatrolEventHandleDto.getHandleDeptId());
//        optHandleFlowRecordAudit.setHandleDeptName(optPatrolEventHandleDto.getHandleDeptName());
        Date createDate = new Date();
        createDate.setTime(createDate.getTime() + 10);
        optHandleFlowRecordAudit.setCreateTime(createDate);
        optHandleFlowRecordAudit.setContent("系统自动");
        optHandleFlowRecordAudit.setCreateBy(SecurityUtils.getUsername());
        Date auditTimeLimit = new Date();
        auditTimeLimit.setHours(auditTimeLimit.getHours() + 1);
        optHandleFlowRecordAudit.setHandleTimeLimit(auditTimeLimit);
        optHandleFlowRecordService.save(optHandleFlowRecordAudit);

        OptPatrolEvent optPatrolEventAudit = getById(optPatrolEventHandleDto.getEvtId());
        optPatrolEventAudit.setStatus(17L);//主管单位审核

        optPatrolEventAudit.setAuditTimeLimit(auditTimeLimit);
        optPatrolEventAudit.setUpdateTime(new Date());
        optPatrolEventAudit.setUpdateBy(SecurityUtils.getUsername());
        return updateById(optPatrolEventAudit);
    }

    @Override
    public boolean audit(OptPatrolEventAuditDto optPatrolEventAuditDto) {
        if (optPatrolEventAuditDto.getIsAuditPass() == 2) {
            //审核不通过
            OptPatrolEvent optPatrolEvent = getById(optPatrolEventAuditDto.getEvtId());
            //插入流程表记录——主管单位审核
            OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
            optHandleFlowRecord.setBusinessId(optPatrolEventAuditDto.getEvtId());
            optHandleFlowRecord.setActivityName("审核反馈不通过");
            optHandleFlowRecord.setActivityFlag(18L);
            if (optPatrolEvent.getObjectId() == null) {
                optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            }

            optHandleFlowRecord.setHandleDeptId(optPatrolEventAuditDto.getAuditDeptId());
            optHandleFlowRecord.setHandleDeptName(optPatrolEventAuditDto.getAuditDeptName());
            optHandleFlowRecord.setCreateTime(new Date());
            //optHandleFlowRecord.setHandleTime(new Date());
            optHandleFlowRecord.setContent(optPatrolEventAuditDto.getContent());
            optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
            optHandleFlowRecordService.save(optHandleFlowRecord);

            //设备自动报警
            if (optPatrolEvent.getSourceId() == 2) {
                //插入流程表记录——主管单位审核
                OptHandleFlowRecord optHandleFlowRecordConfirm = new OptHandleFlowRecord();
                optHandleFlowRecordConfirm.setActivityName("事件确认");
                optHandleFlowRecordConfirm.setActivityFlag(13L);
                if (optPatrolEvent.getObjectId() == null) {
                    optHandleFlowRecordConfirm.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                    optHandleFlowRecordConfirm.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
                } else {
                    optHandleFlowRecordConfirm.setHandleUserId(optPatrolEvent.getObjectId());//当前登录用户Id
                    optHandleFlowRecordConfirm.setHandleUserName(optPatrolEvent.getObjectName());//当前登录用户姓名
                }
                //optHandleFlowRecordConfirm.setHandleTime(new Date());
                optHandleFlowRecordConfirm.setCreateTime(new Date());
                optHandleFlowRecordConfirm.setContent("主管单位审核不通过，系统自动指派到事件处置环节之前");
                optHandleFlowRecordConfirm.setCreateBy(SecurityUtils.getUsername());
                optHandleFlowRecordService.save(optHandleFlowRecordConfirm);

                optPatrolEvent.setStatus(13L);//事件确认
                //optPatrolEvent.setDisposeFinishTime(new Date());//处置完成时间
                optPatrolEvent.setDisposeDeptId(optPatrolEventAuditDto.getAuditDeptId());//处置单位Id
                optPatrolEvent.setDisposeDeptName(optPatrolEventAuditDto.getAuditDeptName());//处置单位名称
                optPatrolEvent.setObjectId(optPatrolEventAuditDto.getAuditUserId());//处置人
                optPatrolEvent.setObjectName(optPatrolEventAuditDto.getAuditUserName());//处置人
                optPatrolEvent.setUpdateTime(new Date());
                optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
                return updateById(optPatrolEvent);
            } else {
                //插入流程表记录——主管单位审核
                OptHandleFlowRecord optHandleFlowRecordDispatch = new OptHandleFlowRecord();
                optHandleFlowRecordDispatch.setActivityName("派遣人员");
                optHandleFlowRecordDispatch.setActivityFlag(12L);
                if (optPatrolEvent.getObjectId() == null) {
                    optHandleFlowRecordDispatch.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                    optHandleFlowRecordDispatch.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
                } else {
                    optHandleFlowRecordDispatch.setHandleUserId(optPatrolEvent.getObjectId());//当前登录用户Id
                    optHandleFlowRecordDispatch.setHandleUserName(optPatrolEvent.getObjectName());//当前登录用户姓名
                }
                optHandleFlowRecordDispatch.setHandleTime(new Date());
                optHandleFlowRecordDispatch.setCreateTime(new Date());
                optHandleFlowRecordDispatch.setContent("主管单位审核不通过，系统自动指派到事件处置环节之前");
                optHandleFlowRecordDispatch.setCreateBy(SecurityUtils.getUsername());
                optHandleFlowRecordService.save(optHandleFlowRecordDispatch);

                optPatrolEvent.setStatus(12L);//派遣人员
                //optPatrolEvent.setDisposeFinishTime(new Date());//处置完成时间
                optPatrolEvent.setDisposeDeptId(optPatrolEventAuditDto.getAuditDeptId());//处置单位Id
                optPatrolEvent.setDisposeDeptName(optPatrolEventAuditDto.getAuditDeptName());//处置单位名称
                optPatrolEvent.setObjectId(optPatrolEventAuditDto.getAuditUserId());//处置人
                optPatrolEvent.setObjectName(optPatrolEventAuditDto.getAuditUserName());//处置人
                optPatrolEvent.setUpdateTime(new Date());
                optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
                return updateById(optPatrolEvent);
            }
        } else {
            OptPatrolEvent optPatrolEvent = getById(optPatrolEventAuditDto.getEvtId());
            //插入流程表记录——主管单位审核
            OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
            optHandleFlowRecord.setBusinessId(optPatrolEventAuditDto.getEvtId());
            optHandleFlowRecord.setActivityName("审核反馈");
            optHandleFlowRecord.setActivityFlag(18L);
            if (optPatrolEvent.getObjectId() == null) {
                optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            }

            optHandleFlowRecord.setHandleDeptId(optPatrolEventAuditDto.getAuditDeptId());
            optHandleFlowRecord.setHandleDeptName(optPatrolEventAuditDto.getAuditDeptName());
            optHandleFlowRecord.setCreateTime(new Date());
            optHandleFlowRecord.setHandleTime(new Date());
            optHandleFlowRecord.setContent(optPatrolEventAuditDto.getContent());
            optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
            optHandleFlowRecordService.save(optHandleFlowRecord);

            optPatrolEvent.setStatus(18L);//审核反馈
            optPatrolEvent.setAuditPersonId(SecurityUtils.getUserId());//当前登录用户Id
            optPatrolEvent.setAuditPersonName(SecurityUtils.getUsername());//当前登录用户姓名
            optPatrolEvent.setCompetentDeptId(optPatrolEventAuditDto.getAuditDeptId());
            optPatrolEvent.setCompetentDeptName(optPatrolEventAuditDto.getAuditDeptName());
            optPatrolEvent.setUpdateTime(new Date());
            optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
            return updateById(optPatrolEvent);
        }
    }

    @Override
    public boolean end(OptPatrolEventEndDto optPatrolEventEndDto) {
        OptPatrolEvent optPatrolEvent = getById(optPatrolEventEndDto.getEvtId());
        //插入流程表记录——事件完结
        OptHandleFlowRecord optHandleFlowRecordEnd = new OptHandleFlowRecord();
        optHandleFlowRecordEnd.setBusinessId(optPatrolEventEndDto.getEvtId());
        optHandleFlowRecordEnd.setActivityName("事件完结");
        optHandleFlowRecordEnd.setActivityFlag(19L);
        optHandleFlowRecordEnd.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
        optHandleFlowRecordEnd.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
        Date createDate = new Date();
        createDate.setTime(createDate.getTime() + 10);
        optHandleFlowRecordEnd.setFinishTime(createDate);
        optHandleFlowRecordEnd.setCreateTime(createDate);
        optHandleFlowRecordEnd.setContent(optPatrolEventEndDto.getContent());
        optHandleFlowRecordEnd.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecordEnd);

        optPatrolEvent.setStatus(19L);//事件完结
        optPatrolEvent.setEndEvtTime(new Date());
        optPatrolEvent.setEndEvtResult(optPatrolEventEndDto.getFeedbackResult());
        optPatrolEvent.setUpdateTime(new Date());
        optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
        boolean success = updateById(optPatrolEvent);
        // 如果含有报警记录id 则更新报警表处置状态为已清除 2023-01-03 liqingbo
        if (ObjectUtil.isNotNull(optPatrolEvent.getAlarmId())) {
            iOptDeviceAlarmService.updateConfirmAlarmToClear(optPatrolEvent.getAlarmId());
        }
        Integer state = optPatrolEvent.getState();
        // 如果含有断面报警记录id 则更新断面报警表处置状态为处置中 2023-01-05 liqingbo
        if (ObjectUtil.isNotNull(optPatrolEvent.getMinorId()) && state != null) {
            if (state == 0) {//排口
                smdtvOutletProblemService.updateConfirmAlarmToInDisposal(optPatrolEvent.getMinorId(), 2);
            } else if (state == 1) {//溢流井
                smdtvOverflowWellService.updateConfirmAlarmToInDisposal(optPatrolEvent.getMinorId(), 2);
            } else if (state == 2) {
                exteriorSiteAlarmService.updateConfirmAlarmToInDisposal(optPatrolEvent.getMinorId(), 2);
            }
        }
        return success;
    }
}
